﻿Imports Business
Imports System.IO
Imports System.Globalization

Public Class frmFingerKeeper
    Private fnCommHandleIndex As Long
    Public grdTemp As DataGridView
    Public dtTable As DataTable = Nothing
    Public strServer As String

    Public Sub New(ByVal gnCommHandleIndex As Long, ByVal strServer As String)

        fnCommHandleIndex = gnCommHandleIndex
        strServer = strServer
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        '/******************************************************************/
        '/*                  Do du lieu len cboSearchKey                   */
        '/******************************************************************/
        cboSearchingKey.Items.Add("Enroll Number")
        cboSearchingKey.Items.Add("Privilege")
        cboSearchingKey.Items.Add("Status")
        cboSearchingKey.SelectedIndex = 0

        SetColumn()



    End Sub

    '/******************************************************************/
    '/*                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 */
    '/******************************************************************/
    '/******************************************************************/
    '/*                        TabPage EnrollData                      */
    '/******************************************************************/

    Public Sub SetColumn()
        grdEnrollData.Columns.Add("nonumber", "No.")
        grdEnrollData.Columns.Add("machineNo", "MachineNo")
        grdEnrollData.Columns.Add("enrollNumber", "EnrollNumber")
        grdEnrollData.Columns.Add("backupNumber", "BackupNumber")
        grdEnrollData.Columns.Add("privilege", "Privilege")
        grdEnrollData.Columns.Add("status", "Status")
        grdEnrollData.Columns.Add("enrollData", "EnrollData")
    End Sub

#Region "Load All Enroll"
    '/******************************************************************/
    '/*                  Function load Enroll data                     */
    '/******************************************************************/
    Private Sub btnGetAllEnroll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetAllEnroll.Click
        lblMessage.Text = "Working..."
        Application.DoEvents()
        grdEnrollData.DataSource = Nothing
        grdEnrollData.Columns.Clear()
        SetColumn()

        Dim vEnrollNumber As Integer
        Dim vEnrollName As String
        Dim vBackupNumber As Integer
        Dim vPrivilege As Integer
        Dim vnEnableFlag As Integer
        Dim vnMessRet As Integer
        Dim vTitle As String
        Dim vnResultCode As Integer
        Dim vEnrollData As String = Space(7354)
        Dim vNumber As Integer = 1

        vnResultCode = FK_EnableDevice(fnCommHandleIndex, 0)
        If vnResultCode <> RUN_SUCCESS Then
            lblMessage.Text = "No device"
        End If

        vnResultCode = FK_ReadAllUserID(fnCommHandleIndex)
        If vnResultCode <> RUN_SUCCESS Then
            lblMessage.Text = "No device"
        End If
        While (FK_GetAllUserID(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vnEnableFlag) = 1)
            lblMessage.Text = "Enroll Number" & vEnrollNumber.ToString() & " - " & vBackupNumber
            Application.DoEvents()
            vnResultCode = FK_GetEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData)
            grdEnrollData.Rows.Add({vNumber, fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vnEnableFlag, vEnrollData})
            vNumber += 1
            Application.DoEvents()
        End While
        grdTemp = grdEnrollData
        dtTable = GetDataTableFromDataGrid(grdTemp)
        lblMessage.Text = "Get all Enroll data successfull!"
        Application.DoEvents()
    End Sub
#End Region

#Region "Search Enroll"

    Public Function GetDataTableFromDataGrid(ByVal grdTemp As DataGridView) As DataTable
        Dim dtTemp As New DataTable
        For i As Integer = 0 To grdTemp.ColumnCount - 1
            dtTemp.Columns.Add(grdTemp.Columns(i).HeaderText, System.Type.GetType("System.String"))
        Next
        For j As Integer = 0 To grdTemp.RowCount - 1
            Dim dr As DataRow = dtTemp.NewRow()
            For i As Integer = 0 To grdTemp.ColumnCount - 1
                dr.Item(i) = grdTemp.Rows(j).Cells(i).Value
            Next
            dtTemp.Rows.Add(dr)
        Next
        Return dtTemp
    End Function

    '/******************************************************************/
    '/*                  Function search Enroll data                   */
    '/******************************************************************/
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        lblMessage.Text = "Working..."
        Application.DoEvents()
        If cboSearchingKey.SelectedIndex = 0 Then
            If Trim(txtSearch.Text) <> "" Then
                Dim dv As New DataView(dtTable)
                dv.RowFilter = "EnrollNumber like '" & txtSearch.Text & "' "
                grdEnrollData.Columns.Clear()
                grdEnrollData.DataSource = dv
                lblMessage.Text = ""
                Application.DoEvents()
            Else
                lblMessage.Text = "Please input Machine No."
            End If
        End If
        If cboSearchingKey.SelectedIndex = 1 Then
            If Trim(txtSearch.Text) = "0" Or Trim(txtSearch.Text) = "1" Then
                Dim dv As New DataView(dtTable)
                dv.RowFilter = "Privilege like '" & txtSearch.Text & "' "
                grdEnrollData.Columns.Clear()
                grdEnrollData.DataSource = dv
                lblMessage.Text = ""
                Application.DoEvents()
            Else
                lblMessage.Text = "Privilege is not avaliable! (0-User ; 1-Manager)"
            End If
        End If
        If cboSearchingKey.SelectedIndex = 2 Then
            If Trim(txtSearch.Text) = "1" Or Trim(txtSearch.Text) = "2" Then
                Dim dv As New DataView(dtTable)
                dv.RowFilter = "Status like '" & txtSearch.Text & "' "
                grdEnrollData.Columns.Clear()
                grdEnrollData.DataSource = dv
                lblMessage.Text = ""
                Application.DoEvents()
            Else
                lblMessage.Text = "Status is not avaliable! (1-Enabled ; 2-Disabled)"
            End If
        End If
    End Sub
#End Region

#Region "Valid input"
    '/******************************************************************/
    '/*                  Only input numeric to txtSearch               */
    '/******************************************************************/
    Private Sub txtSearch_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 4
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
#End Region

#Region "Set User"
    '/******************************************************************/
    '/*                         Function Enable User                   */
    '/******************************************************************/
    Private Sub btnEnableUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnableUser.Click
        If grdEnrollData.Rows.Count <> 0 Then
            lblMessage.Text = "Working"
            Application.DoEvents()
            Dim bsFKAttend As New BSFKAttend(strServer)
            If bsFKAttend.SetUser(fnCommHandleIndex, True, grdEnrollData.CurrentRow.Cells(2).Value, grdEnrollData.CurrentRow.Cells(3).Value) Then
                lblMessage.Text = "Successfull"
                grdEnrollData.CurrentRow.Cells(5).Value = 1
            Else
                lblMessage.Text = "No device"
            End If
            Application.DoEvents()
        Else
            lblMessage.Text = "Please choose Enroll Number"
        End If
    End Sub

    Private Sub btnDisableUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisableUser.Click
        If grdEnrollData.Rows.Count <> 0 Then
            lblMessage.Text = "Working"
            Application.DoEvents()
            Dim bsFKAttend As New BSFKAttend(strServer)
            If bsFKAttend.SetUser(fnCommHandleIndex, False, grdEnrollData.CurrentRow.Cells(2).Value, grdEnrollData.CurrentRow.Cells(3).Value) Then
                lblMessage.Text = "Successfull"
                grdEnrollData.CurrentRow.Cells(5).Value = 2
            Else
                lblMessage.Text = "No device"
            End If
            Application.DoEvents()
        Else
            lblMessage.Text = "Please choose Enroll Number"
        End If
    End Sub
#End Region

#Region "Modify Privilege"
    '/******************************************************************/
    '/*                    Function Modify Privilege                   */
    '/******************************************************************/
    Private Sub btnModifyPrivilege_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifyPrivilege.Click
        If grdEnrollData.Rows.Count <> 0 Then
            lblMessage.Text = "Working"
            Application.DoEvents()
            Dim bsFKAttend As New BSFKAttend(strServer)
            If grdEnrollData.CurrentRow.Cells(4).Value = 1 Then
                If bsFKAttend.ModifyPrivilege(fnCommHandleIndex, grdEnrollData.CurrentRow.Cells(2).Value, grdEnrollData.CurrentRow.Cells(3).Value, 0) Then
                    lblMessage.Text = "Successfull"
                    grdEnrollData.CurrentRow.Cells(4).Value = 0
                Else
                    lblMessage.Text = "No device"
                End If
            Else
                If bsFKAttend.ModifyPrivilege(fnCommHandleIndex, grdEnrollData.CurrentRow.Cells(2).Value, grdEnrollData.CurrentRow.Cells(3).Value, 1) Then
                    lblMessage.Text = "Successfull"
                    grdEnrollData.CurrentRow.Cells(4).Value = 1
                Else
                    lblMessage.Text = "No device"
                End If
            End If
            Application.DoEvents()
        Else
            lblMessage.Text = "Please choose Enroll Number"
        End If
    End Sub
#End Region

#Region "Delete Enroll"
    '/******************************************************************/
    '/*                         Function Delete Enroll                 */
    '/******************************************************************/
    Private Sub btnDeleteEnroll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteEnroll.Click
        If grdEnrollData.Rows.Count <> 0 Then
            lblMessage.Text = "Working"
            Application.DoEvents()
            Dim bsFKAttend As New BSFKAttend(strServer)
            If MessageBox.Show("Confirm", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) = DialogResult.OK Then
                If bsFKAttend.DeleteEnrollData(fnCommHandleIndex, grdEnrollData.CurrentRow.Cells(2).Value, grdEnrollData.CurrentRow.Cells(3).Value) Then
                    lblMessage.Text = "Delete Successfull"
                    grdEnrollData.Rows.Remove(grdEnrollData.CurrentRow)
                Else
                    lblMessage.Text = "No device"
                End If
            End If
            Application.DoEvents()
        Else
            lblMessage.Text = "Please choose Enroll Number"
        End If
    End Sub
#End Region

#Region "Delete All Enroll"
    '/******************************************************************/
    '/*                         Function Empty Enroll                  */
    '/******************************************************************/
    Private Sub btnDeleteAllEnroll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteAllEnroll.Click
        lblMessage.Text = "Working"
        Application.DoEvents()
        Dim bsFKAttend As New BSFKAttend(strServer)
        If MessageBox.Show("Confirm", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) = DialogResult.OK Then
            If bsFKAttend.EmptyEnrollData(fnCommHandleIndex) Then
                lblMessage.Text = "Empty Enroll data Successfull"
                grdEnrollData.Rows.Clear()
            Else
                lblMessage.Text = "No device"
            End If
        End If
        Application.DoEvents()
    End Sub
#End Region

#Region "Save Enroll to file"
    '/******************************************************************/
    '/*                        Function Save Enroll to File            */
    '/******************************************************************/
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAllEnrollData.Click

        Dim vEnrollNumber As Long
        Dim vEnrollName As String
        Dim vBackupNumber As Long
        Dim vPrivilege As Long
        Dim vnMessRet As Long
        Dim vstrMess As String
        Dim vTitle As String
        Dim vstrFileName As String
        Dim vnEnableFlag As Long
        Dim vnResultCode As Integer
        Dim vbRet As Boolean
        Dim vEnrollData As String = Space(7354)

        'Declare a SaveFileDialog object
        Dim objSaveFileDialog As New SaveFileDialog
        lblMessage.Text = "Saving..."
        Application.DoEvents()
        'Set the Save dialog properties
        With objSaveFileDialog
            .DefaultExt = "dat"
            .FileName = "EnrollData"
            .Filter = "Text files (*.dat)|*.dat|All files (*.*)|*.*"
            .FilterIndex = 1
            .OverwritePrompt = True
            .Title = "Save File Dialog"
            .InitialDirectory = Application.StartupPath
        End With

        'Show the Save dialog and if the user clicks the Save button,
        'save the file
        If objSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            vstrFileName = objSaveFileDialog.FileName
            MsgBox(vstrFileName)
            vnResultCode = FK_USBWriteAllEnrollDataToFile(fnCommHandleIndex, vstrFileName)
            'lblMessage.Text = "Save file OK"
            'MsgBox(vnResultCode)
            'Application.DoEvents()
            vEnrollName = Space(256)

            vnResultCode = FK_EnableDevice(fnCommHandleIndex, 0)
            If vnResultCode <> RUN_SUCCESS Then
                lblMessage.Text = "No device"
            End If

            vnResultCode = FK_ReadAllUserID(fnCommHandleIndex)
            If vnResultCode <> RUN_SUCCESS Then
                lblMessage.Text = "No device"
            End If
            MsgBox(vnResultCode)
            While (FK_GetAllUserID(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vnEnableFlag) = 1 And FK_GetEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData) = 1)
                'vnResultCode = FK_GetEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData)
                vnResultCode = FK_USBSetOneEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData, vnEnableFlag, vEnrollName)
                'vnResultCode = FK_USBWriteAllEnrollDataToFile(fnCommHandleIndex, )
                'MsgBox(vnResultCode)
            End While

        End If

        'Clean up
        objSaveFileDialog.Dispose()
        objSaveFileDialog = Nothing
    End Sub
#End Region

#Region "GetAllEnrollUSB"
    Private Sub btnGetAllEnrollUSB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetAllEnrollUSB.Click

        Dim vEnrollNumber As Long
        Dim vEnrollName As String
        Dim vBackupNumber As Long
        Dim vPrivilege As Long
        Dim vnEnableFlag As Long
        Dim vTitle As String
        Dim vstrFileName As String
        Dim vnResultCode As Integer
        Dim vEnrollData As String = Space(7354)
        Dim vNumber As Integer = 1

        Dim myFileDialog As OpenFileDialog = New OpenFileDialog()

        lblMessage.Text = "Working..."
        Application.DoEvents()
        grdEnrollData.DataSource = Nothing
        grdEnrollData.Columns.Clear()
        SetColumn()

        With myFileDialog
            .Filter = "DAT Files (*.dat)|*.dat|All Files (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = Application.StartupPath
            .Title = "Open File"
            .CheckFileExists = False
        End With

        If myFileDialog.ShowDialog() = DialogResult.OK Then
            'Console.WriteLine(myFileDialog.FileName)
            vstrFileName = myFileDialog.FileName
            vEnrollName = Space(256)
            vnResultCode = FK_USBReadAllEnrollDataFromFile(fnCommHandleIndex, vstrFileName)
            If vnResultCode <> RUN_SUCCESS Then
                lblMessage.Text = "No device"
                Exit Sub
            End If

            While (FK_USBGetOneEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData, vnEnableFlag, vEnrollName) = 1)
                lblMessage.Text = "Eroll Number " & vEnrollNumber.ToString() & " - " & vBackupNumber
                Application.DoEvents()
                grdEnrollData.Rows.Add({vNumber, fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vnEnableFlag, vEnrollData})
                vNumber += 1
                Application.DoEvents()
            End While


        End If
        myFileDialog = Nothing
        lblMessage.Text = "Get all Enroll data from USB successfull!"
        Application.DoEvents()
    End Sub
#End Region

#Region "SetAllEnrollUSB"
    Private Sub SetAllEnrollUSB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetAllEnrollDataUSB.Click

        Dim vEnrollNumber As Long
        Dim vEnrollName As String
        Dim vBackupNumber As Long
        Dim vPrivilege As Long
        Dim vnEnableFlag As Long
        Dim vTitle As String
        Dim vstrFileName As String
        Dim vnResultCode As Integer
        Dim vEnrollData As String = Space(7354)
        Dim vNumber As Integer = 1

        Dim myFileDialog As OpenFileDialog = New OpenFileDialog()

        lblMessage.Text = "Working..."
        Application.DoEvents()
        grdEnrollData.DataSource = Nothing
        grdEnrollData.Columns.Clear()
        SetColumn()

        With myFileDialog
            .Filter = "DAT Files (*.dat)|*.dat|All Files (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = "C:\"
            .Title = "Open File"
            .CheckFileExists = False
            .InitialDirectory = Application.StartupPath
        End With

        If myFileDialog.ShowDialog() = DialogResult.OK Then
            'Console.WriteLine(myFileDialog.FileName)

            vstrFileName = myFileDialog.FileName
            vEnrollName = Space(256)
            vnResultCode = FK_USBReadAllEnrollDataFromFile(fnCommHandleIndex, vstrFileName)
            If vnResultCode <> RUN_SUCCESS Then
                lblMessage.Text = "No device"
                Exit Sub
            End If
            If MessageBox.Show("Confirm", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) = DialogResult.OK Then
                Dim bsFKAttend As New BSFKAttend(strServer)
                bsFKAttend.EmptyEnrollData(fnCommHandleIndex)
                While (FK_USBGetOneEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData, vnEnableFlag, vEnrollName) = 1)
                    lblMessage.Text = "Eroll Number " & vEnrollNumber.ToString() & " - " & vBackupNumber
                    Application.DoEvents()
                    vnResultCode = FK_PutEnrollDataWithString(fnCommHandleIndex, vEnrollNumber, vBackupNumber, vPrivilege, vEnrollData)
                End While
                lblMessage.Text = "Set all Enroll data from USB successfull!"
            End If
        End If
        myFileDialog = Nothing
        Application.DoEvents()
    End Sub
#End Region

    '/******************************************************************/
    '/*                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 */
    '/******************************************************************/
    '/******************************************************************/
    '/*                           TabPage LogData                      */
    '/******************************************************************/

    Dim dtGLog As New DataTable
    Dim dtSLog As New DataTable
    Public Sub SetColumndtGlog()
        grdLogData.Columns.Add("nonumber", "No.")
        grdLogData.Columns.Add("enrollno", "Enroll No")
        grdLogData.Columns.Add("verifymode", "Verify Mode")
        grdLogData.Columns.Add("inoutmode", "Inout Mode")
        grdLogData.Columns.Add("datetime", "Date Time")
    End Sub

    Public Sub SetColumndtSlog()
        grdLogData.Columns.Add("nonumber", "No.")
        grdLogData.Columns.Add("senrollno", "SEnroll No")
        grdLogData.Columns.Add("genrollno", "GEnroll No")
        grdLogData.Columns.Add("manipulation", "Manipulation")
        grdLogData.Columns.Add("backupno", "Backup No")
        grdLogData.Columns.Add("datetime", "Date Time")
    End Sub

#Region "Get General Log"
    '/******************************************************************/
    '/*                           Get General Log                      */
    '/******************************************************************/
    Private Sub btnReadGLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadGLog.Click
        lblMessageLogData.Text = "Working..."
        Application.DoEvents()
        Dim inout As Object = {"IN", "OUT", "Over IN", "Over OUT"}
        Dim vEnrollNumber As Long
        Dim verifyMode As Integer
        Dim inoutMode As Integer
        Dim dateTime As Date
        Dim vnresult As Integer

        grdLogData.Columns.Clear()
        grdLogData.Rows.Clear()
        SetColumndtGlog()

        Dim numberNo As Integer = 0
        If chkReadMark.Checked = True Then
            vnresult = FK_LoadGeneralLogData(fnCommHandleIndex, 1)
            While FK_GetGeneralLogData(fnCommHandleIndex, vEnrollNumber, verifyMode, inoutMode, dateTime) = 1
                numberNo += 1
                grdLogData.Rows.Add(numberNo, vEnrollNumber, verifyMode, inout(inoutMode), dateTime.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")))
            End While
            If grdLogData.Rows.Count <> 0 Then
                lblMessageLogData.Text = "Get general log data successfull"
            Else
                lblMessageLogData.Text = "Nonexistance data"
            End If
        Else
            vnresult = FK_LoadGeneralLogData(fnCommHandleIndex, 0)
            While FK_GetGeneralLogData(fnCommHandleIndex, vEnrollNumber, verifyMode, inoutMode, dateTime) = 1
                numberNo += 1
                grdLogData.Rows.Add(numberNo, vEnrollNumber, verifyMode, inout(inoutMode), dateTime.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")))
            End While
            If grdLogData.Rows.Count <> 0 Then
                lblMessageLogData.Text = "Get general log data successfull"
            Else
                lblMessageLogData.Text = "Nonexistance data"
            End If
        End If
        Application.DoEvents()
    End Sub
#End Region

#Region "Save General Log"
    '/******************************************************************/
    '/*                          Save General Log                      */
    '/******************************************************************/
    Private Sub btnSaveGLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveGLog.Click

        lblMessageLogData.Text = "Working..."
        Application.DoEvents()

        Dim vstrFileName As String
        Dim vnresult As Integer
        Dim vEnrollNumber As Long
        Dim verifyMode As Integer
        Dim inoutMode As Integer
        Dim dateTime As Date

        'Declare a SaveFileDialog object
        Dim objSaveFileDialog As New SaveFileDialog

        lblMessage.Text = "Saving..."
        Application.DoEvents()
        'Set the Save dialog properties
        With objSaveFileDialog
            .DefaultExt = "dat"
            .FileName = "GeneralLog"
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .OverwritePrompt = True
            .Title = "Save File Dialog"
            .InitialDirectory = Application.StartupPath
        End With
        'Show the Save dialog and if the user clicks the Save button,
        'save the file
        If objSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            vstrFileName = objSaveFileDialog.FileName
            Dim objWriter As New System.IO.StreamWriter(vstrFileName)
            objWriter.WriteLine("No." & vbTab & "EnrNo" & vbTab & "Verify" & vbTab & "Inout" & vbTab & "DateTime")
            vnresult = FK_LoadGeneralLogData(fnCommHandleIndex, 0)
            Dim numberNo As Integer = 0
            While FK_GetGeneralLogData(fnCommHandleIndex, vEnrollNumber, verifyMode, inoutMode, dateTime) = 1
                numberNo += 1
                lblMessageLogData.Text = "Saving " & vEnrollNumber
                Application.DoEvents()
                objWriter.WriteLine(numberNo & vbTab & vEnrollNumber & vbTab & verifyMode & vbTab & inoutMode & vbTab & dateTime.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")))
                Application.DoEvents()
            End While
            lblMessageLogData.Text = "Save successfull!"
            objWriter.Close()
        Else
            lblMessageLogData.Text = "Save error!"
        End If
        Application.DoEvents()
    End Sub
#End Region

#Region "Empty general log"
    Private Sub btnEmptyGLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmptyGLog.Click
        Dim vnResultCode As Integer

        lblMessageLogData.Text = "Working..."
        System.Windows.Forms.Application.DoEvents()

        vnResultCode = FK_EnableDevice(fnCommHandleIndex, 0)
        If vnResultCode <> RUN_SUCCESS Then
            lblMessageLogData.Text = "No device"
            Exit Sub
        End If

        vnResultCode = FK_EmptyGeneralLogData(fnCommHandleIndex)
        lblMessageLogData.Text = "Empty general log successfull"

        FK_EnableDevice(fnCommHandleIndex, 1)
        grdLogData.Rows.Clear()
    End Sub
#End Region

#Region "Read USB general log"
    Private Sub btnReadUSBGLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadUSBGLog.Click

        lblMessageLogData.Text = "Working..."
        Application.DoEvents()

        Dim vstrFileName As String
        Dim vnresult As Integer
        Dim vEnrollNumber As Long
        Dim verifyMode As Integer
        Dim inoutMode As Integer
        Dim dateTime As Date
        Dim inout As Object = {"IN", "OUT", "Over IN", "Over OUT", "--"}

        Dim myFileDialog As OpenFileDialog = New OpenFileDialog()

        lblMessage.Text = "Working..."
        Application.DoEvents()
        grdLogData.Columns.Clear()
        grdLogData.Rows.Clear()
        SetColumndtGlog()

        With myFileDialog
            .Filter = "GLog Files (*.txt)|*.txt|All Files (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = Application.StartupPath
            .Title = "Open File"
            .CheckFileExists = False
        End With

        If myFileDialog.ShowDialog() = DialogResult.OK Then
            'MsgBox(myFileDialog.FileName)
            vstrFileName = myFileDialog.FileName
            Dim objread As New System.IO.StreamReader(vstrFileName)
            Dim line As String
            Dim array() As String
            line = objread.ReadLine()
            line = objread.ReadLine()
            While line <> Nothing
                array = line.Split(vbTab)
                grdLogData.Rows.Add(array(0), array(1), array(2), inout(CInt(array(3))), array(4))
                line = objread.ReadLine()
            End While
            objread.Close()
        End If
        lblMessageLogData.Text = ""
        Application.DoEvents()
    End Sub
#End Region

#Region "Read super log"
    Private Sub btnReadSLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSLog.Click
        '/******************************************************************/
        '/*                           Get super   Log                      */
        '/******************************************************************/
        lblMessageLogData.Text = "Working..."
        Application.DoEvents()
        Dim manipulationName As Object = {"--", "--", "--", "Enroll User", "Enroll Manager", "Delete Fp Data", "Delete Password", "Delete Card Data", "Delete All LogData", "Modify System Info", "Modify System Time", "Modify Log Setting", "Modify Comm Setting", "Pass Time Set", "Door Set Log"}
        Dim backupName As Object = {"FP-0", "FP-1", "FP-2", "FP-3", "FP-4", "FP-5", "FP-6", "FP-7", "FP-8", "FP-9", "PASS", "CARD"}
        Dim sEnrollNumber As Integer
        Dim gEnrollNumber As Integer
        Dim manipulation As Integer
        Dim backupNumber As Integer
        Dim dateTime As Date
        Dim vnresult As Integer

        grdLogData.Columns.Clear()
        grdLogData.Rows.Clear()
        SetColumndtSlog()

        Dim numberNo As Integer = 0
        If chkReadMark.Checked = True Then
            vnresult = FK_LoadSuperLogData(fnCommHandleIndex, 1)
            While FK_GetSuperLogData(fnCommHandleIndex, sEnrollNumber, gEnrollNumber, manipulation, backupNumber, dateTime) = 1
                numberNo += 1
                grdLogData.Rows.Add(numberNo, sEnrollNumber, gEnrollNumber, manipulationName(manipulation), backupName(backupNumber), dateTime.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")))
            End While
            If grdLogData.Rows.Count <> 0 Then
                lblMessageLogData.Text = "Get super log data successfull"
            Else
                lblMessageLogData.Text = "Nonexistance data"
            End If
        Else
            vnresult = FK_LoadSuperLogData(fnCommHandleIndex, 0)
            While FK_GetSuperLogData(fnCommHandleIndex, sEnrollNumber, gEnrollNumber, manipulation, backupNumber, dateTime) = 1
                numberNo += 1
                grdLogData.Rows.Add(numberNo, sEnrollNumber, gEnrollNumber, manipulationName(manipulation), backupName(backupNumber), dateTime.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")))
            End While
            If grdLogData.Rows.Count <> 0 Then
                lblMessageLogData.Text = "Get general log data successfull"
            Else
                lblMessageLogData.Text = "Nonexistance data"
            End If
        End If
        Application.DoEvents()
    End Sub
#End Region

#Region "Save super log"
    '/******************************************************************/
    '/*                           Save super  Log                      */
    '/******************************************************************/
    Private Sub btnSaveSLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSLog.Click
        lblMessageLogData.Text = "Working..."
        Application.DoEvents()

        Dim sEnrollNumber As Integer
        Dim gEnrollNumber As Integer
        Dim manipulation As Integer
        Dim backupNumber As Integer
        Dim dateTime As Date
        Dim vnresult As Integer
        Dim vstrFileName As String

        'Declare a SaveFileDialog object
        Dim objSaveFileDialog As New SaveFileDialog

        lblMessage.Text = "Saving..."
        Application.DoEvents()
        'Set the Save dialog properties
        With objSaveFileDialog
            .DefaultExt = "txt"
            .FileName = "SuperLog"
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .OverwritePrompt = True
            .InitialDirectory = Application.StartupPath
            .Title = "Save File Dialog"
        End With
        'Show the Save dialog and if the user clicks the Save button,
        'save the file
        If objSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            vstrFileName = objSaveFileDialog.FileName
            Dim objWriter As New System.IO.StreamWriter(vstrFileName)
            objWriter.WriteLine("No." & vbTab & "SEnrollNumer" & vbTab & "GEnrollNumber" & vbTab & "Manipulation" & vbTab & "Backup" & vbTab & "Datetime")
            vnresult = FK_LoadSuperLogData(fnCommHandleIndex, 0)
            Dim numberNo As Integer = 0
            While FK_GetSuperLogData(fnCommHandleIndex, sEnrollNumber, gEnrollNumber, manipulation, backupNumber, dateTime) = 1
                numberNo += 1
                lblMessageLogData.Text = "Saving " & sEnrollNumber
                Application.DoEvents()
                objWriter.WriteLine(numberNo & vbTab & sEnrollNumber & vbTab & gEnrollNumber & vbTab & manipulation & vbTab & backupNumber & vbTab & dateTime.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")))
                Application.DoEvents()
            End While
            lblMessageLogData.Text = "Save successfull!"
            objWriter.Close()
        Else
            lblMessageLogData.Text = ""
        End If
        Application.DoEvents()
    End Sub
#End Region

#Region "Empty super log"
    '/******************************************************************/
    '/*                           Empty super Log                      */
    '/******************************************************************/
    Private Sub btnEmptySLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmptySLog.Click
        Dim vnResultCode As Integer

        lblMessageLogData.Text = "Working..."
        System.Windows.Forms.Application.DoEvents()

        vnResultCode = FK_EnableDevice(fnCommHandleIndex, 0)
        If vnResultCode <> RUN_SUCCESS Then
            lblMessageLogData.Text = "No device"
            Exit Sub
        End If

        vnResultCode = FK_EmptySuperLogData(fnCommHandleIndex)
        lblMessageLogData.Text = "Empty super log successfull"

        FK_EnableDevice(fnCommHandleIndex, 1)
        grdLogData.Rows.Clear()
    End Sub
#End Region

#Region "Read USB super log"
    '/******************************************************************/
    '/*                        Read USB super Log                      */
    '/******************************************************************/
    Private Sub btnReadUSBSLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadUSBSLog.Click
        lblMessageLogData.Text = "Working..."
        Application.DoEvents()

        Dim manipulationName As Object = {"--", "--", "--", "Enroll User", "Enroll Manager", "Delete Fp Data", "Delete Password", "Delete Card Data", "Delete All LogData", "Modify System Info", "Modify System Time", "Modify Log Setting", "Modify Comm Setting", "Pass Time Set", "Door Set Log"}
        Dim backupName As Object = {"FP-0", "FP-1", "FP-2", "FP-3", "FP-4", "FP-5", "FP-6", "FP-7", "FP-8", "FP-9", "PASS", "CARD"}
        Dim sEnrollNumber As Integer
        Dim gEnrollNumber As Integer
        Dim manipulation As Integer
        Dim backupNumber As Integer
        Dim dateTime As Date
        Dim vnresult As Integer
        Dim vstrFileName As String

        Dim myFileDialog As OpenFileDialog = New OpenFileDialog()

        lblMessage.Text = "Working..."
        Application.DoEvents()
        grdLogData.Columns.Clear()
        grdLogData.Rows.Clear()
        SetColumndtSlog()

        With myFileDialog
            .Filter = "SLog Files (*.txt)|*.txt|All Files (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = Application.StartupPath
            .Title = "Open File"
            .CheckFileExists = False
        End With

        If myFileDialog.ShowDialog() = DialogResult.OK Then
            'MsgBox(myFileDialog.FileName)
            vstrFileName = myFileDialog.FileName
            Dim objread As New System.IO.StreamReader(vstrFileName)
            Dim line As String
            Dim array() As String
            line = objread.ReadLine()
            line = objread.ReadLine()
            While line <> Nothing
                array = line.Split(vbTab)
                grdLogData.Rows.Add(array(0), array(1), array(2), manipulationName(CInt(array(3))), backupName(CInt(array(4))), array(5))
                line = objread.ReadLine()
            End While
            objread.Close()
        End If
        lblMessageLogData.Text = ""
        Application.DoEvents()
    End Sub
#End Region

    '/******************************************************************/
    '/*                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 */
    '/******************************************************************/

#Region "Cancel and disconnect"
    Private Sub btnCancelLogData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelLogData.Click
        FK_DisConnect(fnCommHandleIndex)
        Me.Close()
    End Sub


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        FK_DisConnect(fnCommHandleIndex)
        Me.Close()
    End Sub
#End Region

End Class